﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Bxas.Utility.Web.Keepers;
using PhotoHsn.BLL.Core;
using System.Web.UI.WebControls;
using PhotoHsn.BLL.Adapters;
using PhotoHsn.BLL.Core;
using System.Diagnostics;
using PhotoHsn.BLL;

//Sami
namespace PhotoHsn.WebApp.Core
{

    public class MemberUser
    {
        public Members Member;
        public List<Roles> FolderRoles;
        public List<Roles> PageRoles;
        public List<Roles> MenuRoles;
        public List<Roles> ProcessingRoles;

        public MemberUser()
        {
            if (Member == null)
                Member = new Members();

            if (FolderRoles == null)
                FolderRoles = new List<Roles>();
            if (PageRoles == null)
                PageRoles = new List<Roles>();
            if (MenuRoles == null)
                MenuRoles = new List<Roles>();
            if (ProcessingRoles == null)
                ProcessingRoles = new List<Roles>();
        }
    }

    public partial class BasePage : Page
    {
        protected String ServerName = "http:\\localhost";
        public MemberUser memberUser = null;
        public BasePage()
        {
        }

        /// <summary>
        /// Connection string erişimi
        /// </summary>
        public string ConStr
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["dbConnection"];
            }
        }

        protected void throw_out_user()
        {
            Bxas.Utility.Web.Keepers.SessionKeeper.clear_all();
            HttpContext.Current.Response.Redirect("~/Login.aspx");
        }

        #region Overlay
        protected void close_overlay(UpdatePanel up)
        {
            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + "close_overlay_up", "close_progressing_modal();", true);
        }

        protected void close_overlay()
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + "close_overlay", "close_progressing_modal();", true);
        }
        #endregion


        String[] path;
        List<String> folderNames;
        String pagePath;
        String pageName;
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                path = Request.Path.ToString().Split('/');
                folderNames = new List<string>();

                for (int i = 1; i < path.Count() - 1; i++)
                {
                    folderNames.Add(path[i]);
                }
                pagePath = path[path.Count() - 1];
                pageName = pagePath.Substring(0, pagePath.Count() - 5);
                if (SessionKeeper.get_value(SessionNames.Message) != null)
                {
                    MessageShow(SessionKeeper.get_value(SessionNames.Message).ToString(), (EnumMessageTypes)SessionKeeper.get_value(SessionNames.EnumMessageTypes));
                    SessionKeeper.remove(SessionNames.Message);
                    SessionKeeper.remove(SessionNames.EnumMessageTypes);
                }

                if (SessionKeeper.get_value(SessionNames.MemberUser) != null)
                {
                    memberUser = (MemberUser)SessionKeeper.get_value(SessionNames.MemberUser);

                }

                if (!AuthorityControl())
                {
                    if (folderNames.Count > 0)
                    {
                        SessionKeeper.add_is_not_contain(SessionNames.Message, "Girmek İstediğiniz Sayfaya Yetkiniz Yok.");
                        SessionKeeper.add_is_not_contain(SessionNames.EnumMessageTypes, EnumMessageTypes.Uyari);
                        Response.Redirect("~/Default.aspx");
                    }
                }

            }

            base.OnLoad(e);
        }


        public Boolean AuthorityControl()
        {
            if (memberUser == null) return false;
            if (folderNames.Count != 0)
            {
                if (memberUser.FolderRoles != null)
                    foreach (Roles item in memberUser.FolderRoles)
                    {
                        foreach (var item1 in folderNames)
                        {
                            if (item.Name.ToLower() == item1.ToLower())
                            {
                                return true;
                            }
                        }

                    }
            }
            else
                return true;

            if (memberUser.PageRoles != null)
                foreach (Roles item in memberUser.PageRoles)
                {

                    if (item.Name.ToLower() == pageName.ToLower())
                    {
                        return true;
                    }

                }


            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="messageType"></param>
        /// <param name="showTime"> mili saniye olacak</param>
        public void MessageShow(String message, EnumMessageTypes messageType)
        {
            Control form = this.Master.FindControl("masterform");

            Label css = (Label)this.Master.FindControl("lblCss");

            string mesajResim = "";

            switch (messageType)
            {
                case EnumMessageTypes.Hata:
                    mesajResim = "hata.jpg";
                    break;
                case EnumMessageTypes.Bilgi:
                    mesajResim = "bilgi.jpg";
                    break;
                case EnumMessageTypes.Uyari:
                    mesajResim = "uyari.png";
                    break;
                case EnumMessageTypes.Onay:
                    mesajResim = "onay.png";
                    break;

                default:
                    break;
            }

            string cssS = @" <div id='dialog-message' title='"+messageType.ToString()+@"'>
	                            <p>
		                        <span class='ui-icon ui-icon-circle-check' style='float:left; margin:0 7px 50px 0;'></span>
		                        "+message+@"    
	                            </p>	                           
                            </div>";
           
            css.Text = cssS;
            string function = @" $(function () {  
                                    $('#dialog-message').dialog({
                                            resizable: false,
                                            autoOpen: false,
                                            modal: true,
                                            buttons: {
                                                Ok: function () {
                                                    $(this).dialog('close');
                                                }
                                            }
                                    });
                                    $( '#dialog-message' ).dialog( 'open' );
                                });";
            ScriptManager.RegisterStartupScript(form, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
            SessionKeeper.remove(SessionNames.Message);
            SessionKeeper.remove(SessionNames.EnumMessageTypes);
        }

        public void MessageShow(BxasException ex)
        {
            switch (ex.HataTuru)
            {
                case Bxas.Core.Exceptions.EnumaHataTuru.sql:
                    MessageShow(ex.ResolveMessage, EnumMessageTypes.Hata);
                    break;
                case Bxas.Core.Exceptions.EnumaHataTuru.web:
                    MessageShow(ex.ResolveMessage, EnumMessageTypes.Uyari);
                    break;
                default:
                    break;
            }
        }
        

        public String ErrorLogInsert(Exception p_ex, object p_class, StackFrame p_function)
        {
            return "";// new BaseBs().ErrorLogInsert(p_ex, p_class, p_function).Message;
        }
    }
}